<?php
class Default_Model_Users extends Zend_Db_Table {
    protected $_name    =   'users';
    protected $_primary    =   'id';
    
	public function countItem($arrParam = null, $options = null) {
            $db = Zend_Registry::get('connectDb');
//        $db = Zend_Db::factory($adapter, $config);
            $ssFilter = $arrParam['ssFilter'];

            $select = $db->select()
                                    ->from('users AS u', array('COUNT(u.id) AS totalItem'));
            if(!empty($ssFilter['keywords'])){
                    $keywords = '%'.$ssFilter['keywords'].'%';
                    $select->where('u.user_name LIKE ?', $keywords, STRING);
            }
            if($ssFilter['group_id'] > 0){
                $select->where('u.group_id = ?', $ssFilter['group_id'], INTEGER);
            }
            $result = $db->fetchOne($select);
            return $result;
	}
	
	public function sortItem($arrParam = null, $options = null) {
		$cid	=	$arrParam['cid'];
		$order	=	$arrParam['order'];
		if(count($cid) > 0){
			foreach ($cid as $key => $val){
				$where	=	' id = ' . $val;
				$data	=	array('order' => $order[$val]);
				$this->update($data, $where);
			}
		}
	}
	
    public function listItem($arrParam = null, $options = null) {
        $db = Zend_Registry::get('connectDb');
//        $db = Zend_Db::factory($adapter, $config);
		$paginator	= $arrParam['paginator'];
		
		$ssFilter = $arrParam['ssFilter'];
        
        if($options['task'] == 'admin-list'){
            $select = $db->select()
                         ->from('users AS u', array('user_name', 'status', 'email', 'register_date', 'id'))
                         ->joinLeft('user_group AS g', 'g.id = u.group_id', 'group_name');
            
            if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
                    $select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
            }
            if($paginator['ItemCountPerPage'] > 0){
                    $page	=	$paginator['currentPage'];
                    $rowCount = $paginator['ItemCountPerPage'];
                    $select->limitPage($page, $rowCount);
            }
            if(!empty($ssFilter['keywords'])){
                    $keywords = '%'.$ssFilter['keywords'].'%';
                    $select->where('u.user_name LIKE ?', $keywords, STRING);
            }
            if($ssFilter['group_id'] > 0){
                $select->where('u.group_id = ?', $ssFilter['group_id'], INTEGER);
            }
            $result = $db->fetchAll($select);
        }
        return $result;
    }
    
    public function saveItem($arrParam = null, $options = null) {

        if($options['task'] == 'admin-add'){
            $row = $this->fetchNew();
            $row->user_name     =   $arrParam['user_name'];
            $row->password      =   Zendvn_Encode::password($arrParam['password']);
            $row->email         =   $arrParam['email'];
            $row->group_id      =   $arrParam['group_id'];
            $row->first_name    =   $arrParam['first_name'];
            $row->last_name     =   $arrParam['last_name'];
            $row->birthday      =   $arrParam['birthday'];
            $row->status        =   $arrParam['status'];
            $row->sign          =   $arrParam['sign'];
            $row->user_avatar   =   $arrParam['user_avatar'];
            $row->register_date =   date('Y-m-d');
            $row->register_ip   =   $_SERVER['REMOTE'];
            $row->save();
        }
        
        if($options['task'] == 'admin-edit'){
            $where  = "id = " . $arrParam['id'];
            $row = $this->fetchRow($where);
            $row->user_name     =   $arrParam['user_name'];
            if(!empty($arrParam['password'])){
                $row->password      =   Zendvn_Encode::password($arrParam['password']);
            }
            $row->email         =   $arrParam['email'];
            $row->group_id      =   $arrParam['group_id'];
            $row->first_name    =   $arrParam['first_name'];
            $row->last_name     =   $arrParam['last_name'];
            $row->birthday      =   $arrParam['birthday'];
            $row->status        =   $arrParam['status'];
            $row->sign          =   $arrParam['sign'];
            $row->user_avatar   =   $arrParam['user_avatar'];
            $row->register_date =   date('Y-m-d');
            $row->register_ip   =   $_SERVER['REMOTE'];
            $row->save();
        }
    }
    
    public function getItem($arrParam = null, $options = null) {
        if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
            $db = Zend_Registry::get('connectDb');
//            $db = Zend_Db::factory($adapter, $config);
            $select = $db->select()
                        ->from('users as u')
                        ->joinLeft('user_group as g', 'u.group_id = g.id', array('g.group_name'))
                        ->where('u.id = ? ', $arrParam['id'], INTEGER);
            $result = $db->fetchRow($select);
        }
        
        if($options['task'] == 'delete'){
            $where = " id = " . $arrParam['id'];
            $result = $this->fetchRow($where)->toArray();
        }
        return $result;
    }
    
    public function deleteItem($arrParam = null, $options = null) {
        if($options['task'] == 'admin-delete'){
            
            //Lay ten hinh anh user_avatar
            $row = $this->getItem($arrParam , array('task'=>'delete'));

            //Xoa user_avatar
            $upload_dir = FILES_PATH . '/users';

            $upload = new Zendvn_File_Upload();
            $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
            $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
            $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);
            
            $where  = 'id = ' . $arrParam['id'];
            $this->delete($where);
        }
		
        if($options['task'] == 'admin-multi-delete'){
            $cid = $arrParam['cid'];
        
            if(count($cid > 0)){
                foreach ($cid as $key) {
                    $arrParam['id'] = $key;
                    //Lay ten hinh anh user_avatar
                    $row = $this->getItem($arrParam , array('task'=>'delete'));
                    //Xoa user_avatar
                    $upload_dir = FILES_PATH . '/users';
                    $upload = new Zendvn_File_Upload();
                    $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
                    $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
                    $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);
                }
                $ids    = implode(',', $cid);
                $where  = 'id IN (' . $ids . ')';
                $this->delete($where);
            }
        }
    }
    
    public function changeStatus($arrParam = null, $options = null) {
        $cid = $arrParam['cid'];

        if(count($cid) > 0){
            if($arrParam['type']  == 1){
                $status = 1;
            }  else {
                $status = 0;
            }
            $ids    = implode(',', $cid);
            $data   = array('status' => $status);
            $where  = 'id IN (' . $ids . ')';
            $this->update($data, $where);
        }
		
		if($arrParam['id'] > 0){
			if($arrParam['type']  == 1){
				$status = 1;
			}  else {
				$status = 0;
			}

			$data   = array('status' => $status);
			$where  = 'id = ' . $arrParam['id'] ;
			$this->update($data, $where);
		}
    }
}
?>